package com.wanxi.education.service.impl;

import com.wanxi.education.mapper.StatisticMapper;
import com.wanxi.education.model.pojo.CcieClassification;
import com.wanxi.education.model.vo.statistic.CertCategoriesVO;
import com.wanxi.education.model.vo.statistic.CoursePopilarityVO;
import com.wanxi.education.model.vo.statistic.InstitutionalRankingVO;
import com.wanxi.education.model.vo.statistic.QuantityOfOrderVO;
import com.wanxi.education.service.StatisticService;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author: Kang
 * @Date: 2021/9/27 9:14
 * @Description: 后台统计业务逻辑层
 * @Version: 1.0
 */
@Service
public class StatisticServiceImpl implements StatisticService {

    @Autowired
    private StatisticMapper statisticMapper;

    /**
     * 机构注册人数排名前七
     * @return
     */
    @Override
    public Set<InstitutionalRankingVO> institutionalRanking() {
        /**
         * 获取到机构ID，通过机构ID查询所属注册总人数，进行排序，取出前七，最终封装成机构名称和注册总人数
         *  1、查询所有机构，获取到每个机构ID
         *  2、通过机构ID查询到所关联的注册用户，并且这些用户都是学员，剔除机构负责人,获取到每个机构所注册用户的总数
         *  3、讲机构名称和对应注册用户总数进行封装
         */
        Set<InstitutionalRankingVO> institutionalRankingVOSet = statisticMapper.institutionalRanking();
        return institutionalRankingVOSet;
    }

    /**
     * 点击量
     * 课程热度排名前七
     * 1、查询课程和点击量表，将两张表关联，通过课程ID查到匹配的唯一点击量
     * 2、将获取到的课程标题和对应点击量，通过点击量进行降序排序，取出前七即可
     * @return
     */
    @Override
    public Set<CoursePopilarityVO> coursePopularity() {
        Set<CoursePopilarityVO> coursePopilarityVOSet = statisticMapper.coursePopularity();
        for (CoursePopilarityVO coursePopilarityVO : coursePopilarityVOSet) {
            if (coursePopilarityVO.getClick() == null) {
                coursePopilarityVO.setClick(0L);
            }
        }
        return coursePopilarityVOSet;
    }

    /**
     * 订单数量（从当前日期前推一周/天的总量）
     * 1、获取当前日期
     * 2、通过当前日期查询获取到的订单总数
     * 3、当前日期减一，重复第2步骤，直到一周7天为止
     * @return
     */
    @Override
    public List<QuantityOfOrderVO> quantityOfOrder() {
        List<QuantityOfOrderVO> quantityOfOrderVOList = new ArrayList<>(7);
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        for (int i = 6; i >= 0; i--) {
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_MONTH,-i);
            Date thatDayDate = calendar.getTime();
            String format = DateFormatUtils.format(thatDayDate, "yyyy-MM-dd");
            QuantityOfOrderVO quantityOfOrderVO = statisticMapper.quantityOfOrder(format);
            if (quantityOfOrderVO == null) {
                QuantityOfOrderVO orderVO = QuantityOfOrderVO.builder()
                        .thatDayDate(format)
                        .orderTotal(0L)
                        .build();
                quantityOfOrderVOList.add(orderVO);
                continue;
            }
            quantityOfOrderVOList.add(quantityOfOrderVO);
        }
        return quantityOfOrderVOList;
    }

    /**
     * 证书大类的证书数量总量
     * @return
     */
    @Override
    public Set<CertCategoriesVO> numberOfCertCategories() {
        Set<CertCategoriesVO> certCategoriesVOSet = new HashSet<>();
//        获取到所有的证书分类
        List<CcieClassification> ccieClassificationList = statisticMapper.findAllCertType();
//      查找证书大类（根节点0）并存储到 certCategoriesVOSet中
        for (CcieClassification ccieClassification : ccieClassificationList) {
            if (ccieClassification.getPid() == 0) { // 根节点
                CertCategoriesVO categoriesVO = CertCategoriesVO.builder()
                        .id(ccieClassification.getId())
                        .certName(ccieClassification.getName())
                        .build();
                certCategoriesVOSet.add(categoriesVO);
            }
        }
//        通过大类Id查询所属子节点，并进行统计并存储
        for (CertCategoriesVO certCategoriesVO : certCategoriesVOSet) { // 遍历根节点
//            用于记录大类下所属证书总数
            long count = 0;
            for (CcieClassification ccieClassification : ccieClassificationList) { // 遍历所有节点
                if (ccieClassification.getPid() == certCategoriesVO.getId()) { // 匹配所属根节点的子节点并计数
                    count++;
                }
            }
            certCategoriesVO.setCertTotal(count);
        }
        return certCategoriesVOSet;
    }

}
